<?php

namespace App\Http\Controllers;

use App\Services\UserService;
use Illuminate\Http\Request;

class UserController extends Controller
{
    //
    protected $user_serv;
    public function __construct(UserService $user){
        $this->user_serv = $user;
    }

    public function login(Request $request) {
        $info = (array)json_decode($request->get('json'));
        $info = $this->safe_check($info);
        return json_encode($this->user_serv->login($info));
    }


    public function user_info(Request $request) {
        $info = (array)json_decode($request->get('json'));
        $info = $this->safe_check($info);
        return json_encode($this->user_serv->user_info($info));
    }

    public function logout(Request $request) {
        $request->session()->flush(); //清空session
        $request->session()->regenerate(); //重置session_id
        return json_encode($this->user_serv->logout());
    }
    public function view(){
        return json_encode($this->user_serv->view());
    }
    public function add_user(Request $request) {
        $uid = $request->get('uid');
        $name = $request->get('name');

        return json_encode($this->user_serv->add_user($uid,$name));
    }



    public function status(Request $request) {
        if($request->session()->has('id')) {
            $username = $request->session()->get('username');
            $name = $request->session()->get('name');
            return $this->message('1030', '已登录', ['name'=> $name,'username'=> $username]);
        }
        return $this->message('1031', '未登录');
    }

    public function scan(Request $request){
        $user = $request->get('id');
        return $this->user_serv->scan($user);
    }

    public function access_view(Request $request){
        $type = $request->get('type');//0为全部 1为今天 3为3天
        $key = $request->get('key');//搜索关键字
        return json_encode($this->user_serv->access_view($type,$key));
    }

    /**
     * @param Request $request
     * @return string
     */
    public function analysis_view(Request $request){
        $type = $request->get('type');//0为全部 1为今天 3为3天
        $key = $request->get('key');//搜索关键字
        return json_encode($this->user_serv->analysis_view($type,$key));
    }
    public function now_view(Request $request){
        return json_encode($this->user_serv->now_view());
    }
}
